Skip to content

Fix generator emitting invalid C# for nullable activity input types#763

Merged
YunchuWang merged 1 commit into
mainfrom
yunchuwang-fix-generator-issue-730
Jul 15, 2026
Merged

Fix generator emitting invalid C# for nullable activity input types#763
YunchuWang merged 1 commit into
mainfrom
yunchuwang-fix-generator-issue-730

Conversation

@YunchuWang

Copy link
Copy Markdown
Member

Summary

Fixes #730.

Microsoft.DurableTask.Generators produced code that does not compile when a class-based activity uses a nullable input type such as object?. For example:

[DurableTask]
public class WorkerCapacityReviewActivity : TaskActivity<object?, WorkerCapacityReviewResult> { ... }

generated:

public static async Task<WorkerCapacityReviewResult> WorkerCapacityReviewActivity(
    [ActivityTrigger] object? input = default, string instanceId, FunctionContext executionContext)

The = default makes input optional, but it is followed by the required instanceId and executionContext parameters — an optional parameter cannot precede required ones (CS1737), so the generated file fails to compile.

Root cause

AddActivityFunctionDeclaration in DurableTaskSourceGenerator.cs appended = default to the [ActivityTrigger] parameter whenever the input type ended in ?. Unlike the *CallAsync helpers, the trigger parameter is followed by required parameters, so a default value is both invalid and unnecessary (the Functions runtime always binds the input).

Fix

  • Remove the = default append in AddActivityFunctionDeclaration only, with a comment explaining why this method differs from the *CallAsync helpers.
  • The three other = default sites (ScheduleNew…InstanceAsync, Call…Async sub-orchestrator/activity helpers) are intentionally left unchanged: their input is followed by an optional options parameter, so input = default, options = null is valid C#.

Tests

  • Extended the Activities_ClassBasedSyntax theory with an object? input case (added #nullable enable to the input snippet). The expected output verifies the asymmetry: the CallMyActivityAsync helper keeps object? input = default (valid — followed by optional options), while the [Function] trigger emits object? input (no default).
  • Verified the new case fails before the fix (reproduces CS1737 / signature mismatch) and passes after.
  • Full Generators.Tests suite: 89/89 passing.

Copilot AI review requested due to automatic review settings July 15, 2026 19:06

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes a source-generator bug in Microsoft.DurableTask.Generators where class-based Durable Functions activity triggers with nullable input types (e.g., object?) were emitted with input = default ahead of required parameters, producing invalid C# (CS1737) and failing compilation.

Changes:

  • Removed nullable-input defaulting (= default) specifically from [ActivityTrigger] function signatures in AddActivityFunctionDeclaration.
  • Extended generator tests to cover object? activity inputs and to assert the intended asymmetry between Call*Async helpers (may keep = default) and trigger functions (must not).
  • Added an Unreleased changelog entry documenting the fix and linking to #730.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
src/Generators/DurableTaskSourceGenerator.cs Stops appending = default to nullable activity trigger inputs to avoid optional-before-required parameters (CS1737).
test/Generators.Tests/AzureFunctionsTests.cs Adds an object? test case and validates correct generated signatures for both helper and trigger methods.
CHANGELOG.md Documents the generator fix in the Unreleased section with issue link.

The source generator appended `= default` to the [ActivityTrigger] input parameter when an activity's input type was nullable (e.g. object?). Because that parameter is followed by the required instanceId and executionContext parameters, this produced an optional parameter ahead of required ones, which is invalid C# (CS1737) and failed to compile.

The generated *CallAsync helpers legitimately keep `= default` because their input is followed by an optional options parameter, so only AddActivityFunctionDeclaration is changed. Extended the Activities_ClassBasedSyntax regression test with an object? case that reproduces the issue.

Fixes #730

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings July 15, 2026 19:18
@YunchuWang YunchuWang force-pushed the yunchuwang-fix-generator-issue-730 branch from fa07be7 to d7b7dcb Compare July 15, 2026 19:18

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

Comment thread src/Generators/DurableTaskSourceGenerator.cs
@YunchuWang YunchuWang merged commit b86b36b into main Jul 15, 2026
10 checks passed
@YunchuWang YunchuWang deleted the yunchuwang-fix-generator-issue-730 branch July 15, 2026 23:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Generator is creating invalid C# when used on an activity function with no input (object?)

4 participants